package com.afc.struts.test.person;

import java.util.Date;
import java.util.List;
import java.util.Vector;

import com.afc.struts.annotations.PossibleValues;
import com.afc.struts.form.ActionForm;
import com.afc.struts.test.data.PersonDao;
import com.afc.struts.test.model.Person;
import com.afc.struts.utils.LabeledBoolean;
import com.afc.struts.utils.StringUtils;

public interface PersonForm extends ActionForm
{
  public int getId();
  public void setId(int aId);

  public String getFirstName();
  public void setFirstName(String aFirstName);

  public String getLastName();
  public void setLastName(String aLastName);

  public Double getSize();
  public void setSize(Double aSize);

  public Date getBirthday();
  public void setBirthday(Date aBirthday);

  @PossibleValues("getPossibleBestFriends")
  public Person getBestFriend();
  public void setBestFriend(Person aBestFriend);
  public List<Person> getPossibleBestFriends();

  public class DefaultPersonForm implements PersonForm
  {
    public DefaultPersonForm()
    {
      possibleBestFriends = new Vector<>();
      possibleBestFriends.add(null);
      possibleBestFriends.addAll(PersonDao.getInstance().retrieveAll());
    }

    public DefaultPersonForm(int aId, String aFirstName, String aLastName, Double aSize, Date aBirthday, Person aBestFriend)
    {
      this();
      id = aId;
      firstName = aFirstName;
      lastName = aLastName;
      size = aSize;
      birthday = aBirthday;
      bestFriend = aBestFriend;
    }

    @Override
    public int getId()
    {
      return id;
    }

    @Override
    public void setId(int aId)
    {
      id = aId;
    }

    @Override
    public String getFirstName()
    {
      return firstName;
    }

    @Override
    public void setFirstName(String aFirstName)
    {
      firstName = aFirstName;
    }

    @Override
    public String getLastName()
    {
      return lastName;
    }

    @Override
    public void setLastName(String aLastName)
    {
      lastName = aLastName;
    }

    @Override
    public Double getSize()
    {
      return size;
    }

    @Override
    public void setSize(Double aSize)
    {
      size = aSize;
    }

    @Override
    public Date getBirthday()
    {
      return birthday;
    }

    @Override
    public void setBirthday(Date aBirthday)
    {
      birthday = aBirthday;
    }

    @Override
    @PossibleValues("getPossibleBestFriends")
    public Person getBestFriend()
    {
      return bestFriend;
    }

    @Override
    public void setBestFriend(Person aBestFriend)
    {
      bestFriend = aBestFriend;
    }

    @Override
    public List<Person> getPossibleBestFriends()
    {
      return possibleBestFriends;
    }

    @Override
    public LabeledBoolean validateForm()
    {
      LabeledBoolean validate = LabeledBoolean.check(StringUtils.isFeed(firstName) && StringUtils.isFeed(lastName), "firstName and lastName are mandatory");
      if (validate.isTrue())
      {
        validate = LabeledBoolean.check(size == null || (size <= 2.50 && size >= 0.60), "size must be between 0.60 and 2.50");
      }
      return validate;
    }

    protected int id;
    protected String firstName;
    protected String lastName;
    protected Double size;
    protected Date birthday;
    protected Person bestFriend;
    protected List<Person> possibleBestFriends;
  }
}
